package kassiopeia.agent.seda.config;

import kassiopeia.agent.seda.stage.Stage;
import kassiopeia.commons.model.task.TaskDescriptor;

import java.util.*;

public class ConfiguredTask {
    private final List<Stage> stages;
    private final List<SedaConnection> connections;
    private final Map<String, List<SedaConnection>> connectionsMapping = new HashMap<String, List<SedaConnection>>();
    private final TaskDescriptor task;

    public ConfiguredTask(List<Stage> stages, List<SedaConnection> connections, TaskDescriptor task) {
        this.stages = stages;
        this.connections = connections;
        this.task = task;
        createConnectionsMapping();
    }

    public List<SedaConnection> allConnections() {
        return connections;
    }

    public List<Stage> allStages() {
        return stages;
    }

    public TaskDescriptor getTask() {
        return task;
    }

    public List<SedaConnection> getConnectionsFor(String instanceId, String outputName) {
        return connectionsMapping.get(instanceId + outputName);
    }

    private void createConnectionsMapping() {
        String key;
        for (SedaConnection connection : connections) {
            key = connection.getFrom() + connection.getOutput();
            if (connectionsMapping.containsKey(key)) {
                connectionsMapping.get(key).add(connection);
            } else {
                List<SedaConnection> connectionsList = new ArrayList<SedaConnection>();
                connectionsList.add(connection);
                connectionsMapping.put(key, connectionsList);
            }
        }
    }
}
